package com.shse.sale.dao;

import com.shse.sale.meta.Content;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by shse on 3/19/2016.
 * 商品dao，暂时未使用join操作，而是在service层实现非数据库字段的赋值
 */
@Repository
public interface ContentDao {

    /**
     * 获取全部商品，为降低消耗，只取必要字段
     *
     * @return 所有商品
     */
    @Select("select id,title,image,price from content")
    public List<Content> getContents();

    /**
     * 通过分页获取商品概况
     *
     * @param startPos 起始条数
     * @param pageSize 获取数目
     * @return 分页商品
     */
    @Select("select id,title,image,price from content limit #{startPos},#{pageSize}")
    public List<Content> getContentsByPage(@Param("startPos") Integer startPos,
                                           @Param("pageSize") Integer pageSize);


    /**
     * 使用left join，选取传入personId未购买的商品概况，带分页
     *
     * @param startPos 起始条数
     * @param pageSize 获取数目
     * @param personId 用户id
     * @return 未购买分页商品
     */
    @Select("select content.id,content.title,content.image,content.price from content" +
            " left join trx" +
            " on content.id = trx.contentId" +
            " where trx.id is null or trx.personId!=#{personId}" +
            " limit #{startPos},#{pageSize}")
    public List<Content> getUnBuyContentsByPage(@Param("startPos") Integer startPos,
                                                @Param("pageSize") Integer pageSize,
                                                @Param("personId") Integer personId);

    /**
     * 获取传入用户已购买商品
     *
     * @param personId 用户id
     * @return 用户购买商品
     */
    @Select("select content.id,content.title,content.image,trx.time,trx.price" +
            " from content" +
            " left join trx" +
            " on content.id = trx.contentId" +
            " where trx.personId=#{personId}")
    @Results(
            {
                    @Result(id = true, column = "id", property = "id"),
                    @Result(column = "title", property = "title"),
                    @Result(column = "image", property = "image"),
                    @Result(column = "time", property = "buyTime"),
                    @Result(column = "price", property = "buyPrice")

            })
    public List<Content> getBuyedContents(@Param("personId") Integer personId);

    /**
     * 获取当前用户未购买商品条目数
     *
     * @param personId 用户id
     * @return 用户未购买商品数目
     */
    @Select("select count(1) from content" +
            " left join trx" +
            " on content.id = trx.contentId" +
            " where trx.id is null or trx.personId!=#{personId}")
    public Integer getUnBuyContentsCount(@Param("personId") Integer personId);

    /**
     * 获取总条目数
     *
     * @return 商品总数目
     */
    @Select("select count(1) from content")
    public Integer getContentsCount();

    /**
     * 通过商品id获取单个商品
     *
     * @param id 商品id
     * @return 单个商品
     */
    @Select("select * from content where id=#{id}")
    public Content getContentById(@Param("id") Integer id);

    /**
     * 添加商品，生成主键，并对传入商品进行主键设置
     *
     * @param content 商品对象
     */
    @Insert("insert into content(title,summary,detail,image,price) values(#{title},#{summary},#{detail},#{image},#{price})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void addContent(Content content);

    /**
     * 更新商品
     *
     * @param content 商品对象
     */
    @Update("update content set title=#{title}, summary=#{summary}, detail=#{detail}, image=#{image}, price=#{price} where id=#{id}")
    void updateContent(Content content);

    /**
     * 通过商品id删除商品
     *
     * @param id 商品id
     */
    @Delete("delete from content where id=#{id}")
    public void deleteContent(String id);

}
